package com.sxpi.course.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.map.MapUtil;
import com.sxpi.common.result.R;
import com.sxpi.course.feign.api.CourseServiceFeign;
import com.sxpi.course.vo.CoursePriseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;

/**
 * @Author: 董伟豪
 * @Date: 2022/11/16 20:27
 */
@RestController
@Slf4j
public class CoursePriseController extends BaseController{

    @Autowired
    private CourseServiceFeign courseServiceFeign;

    /**
     * 保存用户点赞信息
     * 并且同步点赞数
     * @param coursePriseVo
     * @return
     */
    @PostMapping("/parise/save")
    @SaCheckLogin
    public R saveCoursePrise(@RequestBody CoursePriseVo coursePriseVo) {
        Object loginId = StpUtil.getLoginId();
        long userId = Long.parseLong(String.valueOf(loginId));
        Map<String, Object> dataMap = StpUtil.getSession().getDataMap();
        String username = MapUtil.getStr(dataMap, "username");
        String avatar = MapUtil.getStr(dataMap, "avatar");
        coursePriseVo.setUserId(userId);
        coursePriseVo.setUseravatar(avatar);
        coursePriseVo.setUsername(username);
        coursePriseVo.setStatus(1);
        coursePriseVo.setIsActive(1);
        // 保存点赞信息
        R save = courseServiceFeign.insertCoursePrise(coursePriseVo);
        Boolean flag = MapUtil.getBool(save, "flag");
        Integer count = 0;
        if(flag){
            // 查询点赞信息
            R r = courseServiceFeign.countByCourseId(coursePriseVo.getCourseId());
            count = MapUtil.getInt(r, "count");
            // 缓存同步
        }
        return save.put("parseCount",count);
    }

    /**
     * 保存用户点赞信息 锁
     * 并且同步点赞数
     * @param coursePriseVo
     * @return
     */
    @PostMapping("/parise/savelock")
    @SaCheckLogin
    public R saveCoursePriseLock(@RequestBody CoursePriseVo coursePriseVo) {
        Object loginId = StpUtil.getLoginId();
        long userId = Long.parseLong(String.valueOf(loginId));
        Map<String, Object> dataMap = StpUtil.getSession().getDataMap();
        String username = MapUtil.getStr(dataMap, "username");
        String avatar = MapUtil.getStr(dataMap, "avatar");
        coursePriseVo.setUserId(userId);
        coursePriseVo.setUseravatar(avatar);
        coursePriseVo.setUsername(username);
        coursePriseVo.setStatus(1);
        coursePriseVo.setIsActive(1);
        // 保存点赞信息
        R save = courseServiceFeign.insertCoursePriseLock(coursePriseVo);
        Boolean flag = MapUtil.getBool(save, "flag");
        Integer count = 0;
        if(flag){
            // 查询点赞信息
            R r = courseServiceFeign.countByCourseId(coursePriseVo.getCourseId());
            count = MapUtil.getInt(r, "count");
            // 缓存同步
        }
        return save.put("parseCount",count);
    }


    /**
     * 用户取消课程的点赞记录
     * @param courseId
     * @return
     */
    @PostMapping("/parise/cancel/{courseId}")
    @SaCheckLogin
    public R cancelCoursePrise(@PathVariable("courseId") Long courseId) {
        Object loginId = StpUtil.getLoginId();
        Long userId = Long.parseLong(String.valueOf(loginId));
        R update = courseServiceFeign.cancelCoursePrise(courseId, userId);
        Boolean flag = MapUtil.getBool(update, "flag");
        Integer count = 0;
        if(flag){
            // 查询点赞信息
            R r = courseServiceFeign.countByCourseId(courseId);
            count = MapUtil.getInt(r, "count");
            // 缓存同步
        }
        return update.put("pariseCount",count);
    }

    /**
     * 获取课程的点赞的总数排行
     * @return
     */
    @PostMapping("/parise/rank")
    public R getCoursePriseRank() {
        return courseServiceFeign.getCoursePriseRank();
    }
}
